package lbj.cyw.gulimall.product.service.impl;

import lbj.cyw.common.utils.PageUtils;
import lbj.cyw.common.utils.Query;
import lbj.cyw.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lbj.cyw.gulimall.product.dao.CategoryDao;
import lbj.cyw.gulimall.product.entity.CategoryEntity;
import lbj.cyw.gulimall.product.service.CategoryService;
import org.yaml.snakeyaml.emitter.ScalarAnalysis;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
//使用ServiceImpl的参数baseMapper跟注入categoryDao效果一样, 是一个东西
//    @Autowired
//    CategoryDao categoryDao;

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        //1. 查处所有分类
        List<CategoryEntity> entities = baseMapper.selectList(null);
        //2. 组装父子树形结构
        //2.1 找到所有一级分类
        List<CategoryEntity> level1Menus  = entities.stream().filter(categoryEntity -> categoryEntity.getParentCid().longValue() == 0)
                .map((menu)->{
                    menu.setChildren(getChildrens(menu,entities));
                    return menu;
                })
                .sorted((menu1,menu2)->{
                    return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
                })
                .collect(Collectors.toList());

        return level1Menus ;
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {

        //ToDo 1.检查当前删除的菜单, 是否被别的地方引用
        baseMapper.deleteBatchIds(asList);
    }

    // 回显级联路径信息
    @Override
    public Long[] findCatelogpath(Long catelogId) {
        List<Long> paths = findParentPath(catelogId, new ArrayList<Long>());
        Collections.reverse(paths);

        return paths.toArray(new Long[paths.size()]);
    }
/*
* 更新所有关联的数据
* */
    @Override
    public void updateCascade(CategoryEntity category) {
        this.updateById(category);
        categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());

    }

    private  List<Long> findParentPath(Long catelogId,List<Long> paths){
        paths.add(catelogId);
        CategoryEntity byId = this.getById(catelogId);
        if(byId.getParentCid()!=0){
            findParentPath(byId.getParentCid(),paths);
        }
        return  paths;
    }

    private List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all){
        List<CategoryEntity> child = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid().longValue() == root.getCatId().longValue();
        })
        .map((menu)->{
            menu.setChildren(getChildrens(menu,all));
            return menu;
        })
        .sorted((menu1,menu2)->{
            return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());

        return child;
    }

}